//
//  LBCCategoryMoreVC.m
//  喜马拉雅改良版框架
//
//  Created by dllo on 16/1/4.
//  Copyright © 2016年 蓝鸥科技. All rights reserved.
//

#import "LBCCategoryMoreVC.h"

@interface LBCCategoryMoreVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong)UITableView *allTableView;
@property (nonatomic, strong)NSMutableArray *allArr;

@end

@implementation LBCCategoryMoreVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"全部";
    UIImage *backImage = [UIImage imageNamed:@"icon_back_h@2x.png"];
    backImage = [backImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:backImage style:UIBarButtonItemStyleDone target:self action:@selector(backAction)];
// 分段按钮
    NSArray *array = [NSArray arrayWithObjects:@"最火", @"最新更新", @"经典", nil];
    UISegmentedControl *segmentC = [[UISegmentedControl alloc] initWithItems:array];
    segmentC.backgroundColor = [UIColor whiteColor];
    segmentC.frame = CGRectMake(10, 10, SCREEN_WIDTH - 20, 30);
// 设置按钮选中颜色
    segmentC.tintColor = [UIColor colorWithRed:0.97 green:0.39 blue:0.26 alpha:1];
    [self.view addSubview:segmentC];
    segmentC.selectedSegmentIndex = 0;
// target Action
    [segmentC addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    
    _allTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    _allTableView.delegate = self;
    _allTableView.dataSource = self;
    [self.view addSubview:_allTableView];
    _allTableView.rowHeight = SCREEN_HEIGHT / 7.4;
    [_allTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(@0);
        make.top.equalTo(segmentC.mas_bottom).offset(10);
        
    }];
    
    [_allTableView registerClass:[LBCCategoryListCell class] forCellReuseIdentifier:@"LBCCategoryListCell"];
    
    _allArr = [NSMutableArray array];
    [self getHotData];
    
}


- (void)getHotData{
    NSString *urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/album?calcDimension=hot&categoryId=%@&device=iPhone&pageId=1&pageSize=20&status=0&tagName=%@", _categoryId, _tagName];
    [AFNetworkTool getUrl:urlStr body:nil result:HXJSON headerFile:nil success:^(id result) {
        
        NSMutableArray *array = [result objectForKey:@"list"];
        for (NSMutableDictionary *dic in array) {
            LBCCategoryListModel *listM = [LBCCategoryListModel new];
            [listM setValuesForKeysWithDictionary:dic];
            [_allArr addObject:listM];
        }
        [_allTableView reloadData];
    } failure:^(NSError *error) {
        
        
    }];
    
}

- (void)getRecentData{
    NSString *urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/album?calcDimension=recent&categoryId=%@&device=iPhone&pageId=1&pageSize=20&status=0&tagName=%@", _categoryId, _tagName];
    [AFNetworkTool getUrl:urlStr body:nil result:HXJSON headerFile:nil success:^(id result) {
        
        NSMutableArray *array = [result objectForKey:@"list"];
        for (NSMutableDictionary *dic in array) {
            LBCCategoryListModel *listM = [LBCCategoryListModel new];
            [listM setValuesForKeysWithDictionary:dic];
            [_allArr addObject:listM];
        }
        [_allTableView reloadData];
    } failure:^(NSError *error) {
        
        
    }];

}

- (void)getMostplayData{
    NSString *urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/album?calcDimension=mostplay&categoryId=%@&device=iPhone&pageId=1&pageSize=20&status=0&tagName=%@", _categoryId, _tagName];
    [AFNetworkTool getUrl:urlStr body:nil result:HXJSON headerFile:nil success:^(id result) {
        
        NSMutableArray *array = [result objectForKey:@"list"];
        for (NSMutableDictionary *dic in array) {
            LBCCategoryListModel *listM = [LBCCategoryListModel new];
            [listM setValuesForKeysWithDictionary:dic];
            [_allArr addObject:listM];
        }
        [_allTableView reloadData];
    } failure:^(NSError *error) {
        
        
    }];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _allArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *idntifier = @"LBCCategoryListCell";
    LBCCategoryListCell *listCell = [tableView dequeueReusableCellWithIdentifier:idntifier];
    // 显示cell右侧的箭头
    [listCell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    
    listCell.cateListModel = _allArr[indexPath.row];
    
    return listCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    KBAlbumsDetailViewController *albumsVC = [KBAlbumsDetailViewController new];
    LBCCategoryListModel *listM = _allArr[indexPath.row];
    albumsVC.albumId = [NSString stringWithFormat:@"%ld", listM.albumId];
    [self.navigationController pushViewController:albumsVC animated:YES];
}


- (void)segmentAction:(UISegmentedControl *)segment{
    if (segment.selectedSegmentIndex == 0) {
        [self getHotData];
    } else if(segment.selectedSegmentIndex == 1){
        [self getRecentData];
    } else {
        [self getMostplayData];
    }
}

-(void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
